package jp.sourceforge.akjrcp.text_douga.video;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import jp.sourceforge.akjrcp.text_douga.AudioData;
import jp.sourceforge.akjrcp.text_douga.SystemEnvironment;
import jp.sourceforge.akjrcp.text_douga.VideoData;
import jp.sourceforge.akjrcp.text_douga.DougaPreference;
import jp.sourceforge.akjrcp.text_douga.VideoWriter;
import jp.sourceforge.akjrcp.text_douga.effect.VideoEffect;

public class CopyVideoData extends EffectableVideoData{
	

	private double duration=3.0;
	//private double loopPadTime;//loop only
	//private int loop;
	//private boolean canStopInLoop=true;//don't use
	private AudioData audioData;
	//private int padColor;
	
	
	private boolean fitSize=true;
	private int bgColor=0;
	//TODO align

	public String toString(){
	return "duration="+duration;	
	}
	public CopyVideoData(){
		
	}

	public double getDuration() {
		return duration;
	}
	public void setDuration(double duration) {
		this.duration = duration;
	}



	public int writeData(File dir,DougaPreference preference,int startIndex,int maxframe) throws IOException {
		int wf=0;
		
		if(startEffect!=null){
			startEffect.setKeyFrame(startIndex);
		}
		
		if(startIndex>0){
			File jpegFile=new File(dir,VideoUtils.toFileName("",startIndex-1,5,"jpg"));//TODO change
		
		
		BufferedImage bgImage=ImageIO.read(jpegFile);
		if(bgImage!=null){
			
			int frames=(int) (preference.getFps()*duration);
			if(maxframe!=0 && maxframe<frames){
				frames=maxframe;
			}
			wf=VideoUtils.copyImage(bgImage, dir, startIndex, frames);
			
		}
		}
		//sync audio
		if(audioData!=null){
			audioData.setSynchLength(wf/preference.getFps());
		}
		
		
		//TODO if video is too short ,bad things will happen.
		if(wf>0){
			if(endEffect!=null){
				endEffect.setKeyFrame(startIndex+wf-1);
			}
		}else{//0 writed
			if(startEffect!=null){
				startEffect.setKeyFrame(VideoEffect.NULL_TIME);//no writed.
			}
		}
		
		return wf;
	}
	public AudioData getAudioData() {
		return audioData;
	}
	public void setAudioData(AudioData audioData) {
		this.audioData = audioData;
	}
	public int getBgColor() {
		return bgColor;
	}
	public void setBgColor(int bgColor) {
		this.bgColor = bgColor;
	}
	public boolean isFitSize() {
		return fitSize;
	}
	public void setFitSize(boolean fitSize) {
		this.fitSize = fitSize;
	}

	public double getTotalTime() {
		
		return duration;
	}
	public int writeData(VideoWriter writer, DougaPreference preference) throws IOException {
		int maxframe=Math.max(0, writer.getMaxFrame()-writer.getWrited());
		int startIndex=writer.getWrited()+1;
		
		if(startEffect!=null){
			startEffect.setKeyFrame(startIndex);
		}
		
		int wf=0;
		if(startIndex>1){
			File jpegFile=writer.getWorkingFrameFile(startIndex-1);
		
		
		BufferedImage bgImage=ImageIO.read(jpegFile);
		if(bgImage!=null){
			
			int frames=(int) (preference.getFps()*duration);
			if(maxframe!=0 && maxframe<frames){
				frames=maxframe;
			}
			wf=VideoUtils.copyImage(bgImage, this,writer,  frames);
			
		}
		}
		//sync audio
		if(audioData!=null){
			audioData.setSynchLength(wf/preference.getFps());
		}
		
		
//		TODO if video is too short ,bad things will happen.
		if(wf>0){
			if(endEffect!=null){
				endEffect.setKeyFrame(startIndex+wf-1);
			}
		}else{//0 writed
			if(startEffect!=null){
				startEffect.setKeyFrame(VideoEffect.NULL_TIME);//no writed.
			}
		}
		
		return wf;
	}
}
